package com.example.androidhive;

import java.util.ArrayList;
import java.util.HashMap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.AdapterView;
//import android.widget.ImageView;
//import android.widget.SimpleAdapter;
//import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CustomizedListView extends Activity {

	public final static String SOAP_ACTION = "http://www.alpha.net.gr/ExecuteEBSCommand";

	public final static String OPERATION_NAME = "ExecuteEBSCommand";

	public final static String NAMESPACE = "http://www.alpha.net.gr";

	public final static String URL = "http://192.168.1.246/Ebs2Alpha/Service.asmx";

	// All static variables
	// static final String URL = "http://api.androidhive.info/music/music.xml";
	// XML node keys
	static final String KEY_ITEM = "Items"; // parent node
	static final String KEY_ID = "GID";
	final static String KEY_NAME = "Description";
	static final String KEY_COST = "NumericField1";
	static final String KEY_DESC = "Code";
	static final String pic = "pic";
	public static Bitmap decodedByte = null;
	static final String ICON = "photo";

	ListView list;
	LazyAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
		Request.addProperty("strCommand", "get_items");
		Request.addProperty("strCommandParameters", "%");

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.setOutputSoapObject(Request);
		try {

			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			// this is the actual part that will call the webservice
			androidHttpTransport.call(SOAP_ACTION, soapEnvelope);

			// Get the SoapResult from the envelope body.
			SoapObject result = (SoapObject) soapEnvelope.bodyIn;
			// tv.setText("Customers : " + result);

			ArrayList<HashMap<String, Object>> songsList = new ArrayList<HashMap<String, Object>>();

			XMLParser parser = new XMLParser();
			String xml = result.getProperty(0).toString(); // getting XML from
															// URL
			Document doc = parser.getDomElement(xml); // getting DOM element

			NodeList nl = doc.getElementsByTagName(KEY_ITEM);
			// looping through all song nodes <song>
			for (int i = 0; i < nl.getLength(); i++) {
				// creating new HashMap
				HashMap<String, Object> map = new HashMap<String, Object>();
				Element e = (Element) nl.item(i);
				// adding each child node to HashMap key => value
				map.put(KEY_ID, parser.getValue(e, KEY_ID));
				map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
				map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
				// map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
				// map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

				// adding HashList to ArrayList
				// songsList.add(map);

				SoapObject Request2 = new SoapObject(NAMESPACE, OPERATION_NAME);
				Request2.addProperty("strCommand", "get_item_picture");
				Request2.addProperty("strCommandParameters",
						parser.getValue(e, KEY_ID) + "|100|80");

				// tv.setText("gid: " + parser.getValue(e, KEY_ID));
				SoapSerializationEnvelope soapEnvelope2 = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);
				soapEnvelope2.dotNet = true;
				soapEnvelope2.setOutputSoapObject(Request2);

				try {
					HttpTransportSE androidHttpTransport2 = new HttpTransportSE(
							URL);

					// this is the actual part that will call the webservice
					androidHttpTransport2.call(SOAP_ACTION, soapEnvelope2);

					// Get the SoapResult from the envelope body.
					SoapObject result2 = (SoapObject) soapEnvelope2.bodyIn;

					byte[] decodedString = Base64.decode(result2.getProperty(0)
							.toString(), Base64.DEFAULT);
					decodedByte = BitmapFactory.decodeByteArray(decodedString,
							0, decodedString.length);
					map.put(ICON, decodedByte);

					songsList.add(map);

					// ImageView image = ((ImageView)
					// findViewById(R.id.list_image));
					// creating new HashMap
					// ArrayList<HashMap<Bitmap, Bitmap>> picList = new
					// ArrayList<HashMap<Bitmap, Bitmap>>();

					// HashMap<Bitmap, Bitmap> icon = new HashMap<Bitmap,
					// Bitmap>();
					// Element ic = (Element) nl.item(i);
					// icon.put(decodedByte, decodedByte);

					// adding HashList to ArrayList
					// picList.add(icon);

					// ImageView image = new ImageView(this);
					// image.setImageBitmap(decodedByte);
					// ImageView image = new ImageView(this);
					// image.setImageBitmap(decodedByte);

				} catch (Exception ex) {

					// tv.setText("error: " + e);
					ex.printStackTrace();
				}

			}

			list = (ListView) findViewById(R.id.list);

			// Getting adapter by passing xml data ArrayList
			adapter = new LazyAdapter(this, songsList);
			list.setAdapter(adapter);

			// Click event for single list row
			list.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

				}
			});
		}

		catch (Exception e) {

			// tv.setText("error: " + e);
			e.printStackTrace();

		}
	}
}